package com.hq.controller;

import com.hq.model.Guide;
import com.hq.model.Root;
import com.hq.model.ScenicSpot;
import com.hq.model.Shopping;
import com.hq.service.PassengerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 游客
 */
@Controller
@RequestMapping("/passenger")
public class PassengerController {

    @Autowired
    PassengerService passengerService;

    //查询所有景点
    @RequestMapping("/findSpot")
    String findSpot(Model model) {
        List<ScenicSpot> listSpot = passengerService.findSpot();
        model.addAttribute("ListSpot", listSpot);
        return "back/passenger/scenicSpot";
    }

    //添加到购物车
    @RequestMapping("/goShopping/{scenicSpot}/{price}")
    String goShopping(Model model,
                      HttpSession session,
                      @PathVariable("scenicSpot") String scenicSpot,
                      @PathVariable("price") String price) {
        Root root = (Root) session.getAttribute("root");
        String username = root.getUsername();
        String payment = "否";
        passengerService.goShopping(scenicSpot, price, payment, username);
        return "redirect:/passenger/findSpot";
    }

    //查看购物车
    @RequestMapping("/shoppingCart")
    String shoppingCart(Model model, HttpSession session) {
        Root root = (Root) session.getAttribute("root");
        String username = root.getUsername();
        List<Shopping> shoppingCart = passengerService.queryShoppingCart(username);
        model.addAttribute("ShoppingCart", shoppingCart);
        return "/back/passenger/shoppingCartList";
    }

    //删除购物中商品
    @RequestMapping("/del/{id}")
    String del(@PathVariable("id") int id) {
        passengerService.del(id);
        return "redirect:/passenger/shoppingCart";
    }

    //购买
    @RequestMapping("/buy/{id}")
    String buy(@PathVariable("id") int id, Model model) {
        Shopping shopping = passengerService.goBuy(id);
        List<Guide> guideList = passengerService.findGuide();
        model.addAttribute("GuideList", guideList);
        model.addAttribute("Shopping", shopping);
        return "/back/passenger/buy";
    }

    //订单
    @RequestMapping("/order/{id}")
    String myOrder(@PathVariable("id") int id, Model model) {
        String payment = "是";
        passengerService.myOrder(id, payment);
        Shopping shoppingList = passengerService.goBuy(id);
        model.addAttribute("ShoppingList", shoppingList);
        return "/back/passenger/myOrder";
    }

    //我的订单
    @RequestMapping("/order")
    String order(Model model) {
        String paid = "是";
        List<Shopping> shoppingList = passengerService.paid(paid);
        model.addAttribute("ShoppingList", shoppingList);
        return "/back/passenger/myOrder";
    }
}
